%make background  database
YearMonthDay=20190422;
photoStart=321;
photoEnd=870;
mode=0;
photoBEC='\\RB87BEC2\';
if strcmp(photoBEC,'\\Camera-PC\')
    photoNamePre='4552test_';
    filename_pre='ascOD4552test_';
    resolution=512;
    folderName=[photoBEC 'ShareFolder\' num2str(YearMonthDay) 'morning\4552\'];
    refimagesIniDirectory='D:\Rb87ProgramBEC3\removeBackground\backgrounddata\refimagesIni_4552new.mat';
elseif strcmp(photoBEC,'\\RB87BEC2\')
    photoNamePre='22834test_';
    filename_pre='ascOD22834test_';
    resolution=1024;
    folderName=[photoBEC 'ShareFolder\' num2str(YearMonthDay) 'morning\ikon-22834\'];
    refimagesIniDirectory='D:\Rb87ProgramBEC3\removeBackground\backgrounddata\refimagesIni_22834new.mat';
end

refNum=250;
refInd=photoStart:(photoStart+refNum-1);
if mode==0
    if exist(refimagesIniDirectory,'file')==2
        refimagesIniStruct=load(refimagesIniDirectory);
        refimagesIni=refimagesIniStruct.refimagesIni;
        
        if size(refimagesIni,3)==refNum
            refimages=refimagesIni;
        else
            error('Error: refNum ~= loaded reference images');
        end
    end
    
    
    refInd=photoStart:min((photoStart+refNum-1),photoEnd);
    refimagesIni(:,:,mod(refInd,refNum)+1)=refCreate(refInd,'bg',folderName,photoNamePre,resolution);
    save(refimagesIniDirectory','refimagesIni');
end